<?php
session_start();
include_once('inc.php');
include_once('databaseAccess.php');

echo '
<!DOCTYPE html> 
<html> 
<head> 
	<title font-style="font-family:Lucida Grande">Datenschutz-App</title> 
	<meta name="viewport" content="width=device-width, initial-scale=1"> 
	<link rel="stylesheet" type="text/css" href="../style/style.css" />
	<link rel="shortcut icon" href="../images/favicon.ico">
	<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
	<script>
	
		var allQuestAnswers = new Array();
		var myObject = new Object();
		var tmpArray = new Array();
		var tmpObject = new Object();
	';
	//Kategorien nacheinander durchgehen und alle jeweiligen Fragen dieser Kategorie auslesen
for($i = 0; $i < count( $questionCategoryArray ); $i++) {
	$categoryName = $categoryArray[ $questionCategoryArray[$i]['catnr'] ];
	$questionText = " ";
	for($y = 0; $y < count( $questionsArray ); $y++) {
		if( $questionsArray[$y]['questnr'] == $questionCategoryArray[$i]['questnr'] ) {
			$questionText = $questionsArray[$y]['quest'];
		}
	}
	echo '
		myObject = new Object();
		myObject.questnr = '.$i.';
		myObject.catname = "'.$categoryName.'";
		myObject.questname = "'.$questionText.'";
		tmpArray = new Array();
		';
	$myQuestionNumber = $questionCategoryArray[$i]['questnr'];	
	//Alle Antworten und Punkten dieser Fragen auslesen
	for($y = 0; $y < count( $answersArray ); $y++) {
		if( $answersArray[$y]['questnr'] == $myQuestionNumber )
		echo '
		tmpObject = new Object();
		tmpObject.name = "'.$answersArray[$y]['answ'].'";
		tmpObject.pts = '.$answersArray[$y]['pts'].';
		tmpArray.push( tmpObject );
		';
	}
	echo '
		myObject.answers = tmpArray;
		allQuestAnswers.push( myObject );
	';
}	
	
echo '	
	
	function fillTable() {
		$( "#questionTablePlaceholder" ).empty();
		var Empty = " ";
		var tableHeader = " " +
		"<table width=\"100%\" rules=\"groups\" border=\"1\" bordercolor=\"#CCCCCC\" cellpadding=\"7px\">" +
			"<thead>" +
				"<th>Fragen-Nummer</th>" +
				"<th>Kategorie</th>" +
				"<th>Frage</th>" +
				"<th>Antwort</th>" +
				"<th>Punktzahl</th>" +
			"</thead>";
		Empty = tableHeader;
		var selectedCategory = $( "#myCategory" ).val();
		for(var i = 0; i < allQuestAnswers.length; i++ ) {
			if( allQuestAnswers[i].catname == selectedCategory || selectedCategory == "All" ) {
				// zuerst die Frage
				var questionForTable = " " +  
				"<tr>" +
					"<td valign=\"middle\" align=\"center\">" + allQuestAnswers[i].questnr + "</td>" +
					"<td valign=\"middle\" align=\"center\">" + allQuestAnswers[i].catname + "</td>" +
					"<td valign=\"middle\" align=\"center\">" + allQuestAnswers[i].questname + "</td>" +
					"<td valign=\"middle\" align=\"center\"> </td>" +
					"<td valign=\"middle\" align=\"center\"> </td>" +
				"</tr>";	
				Empty += questionForTable;
				// jetzt die Antworten
				for(var j = 0; j < allQuestAnswers[i].answers.length; j++ ) {
					var answerForTable = " " +  
					"<tr>" +
						"<td valign=\"middle\" align=\"center\"> </td>" +
						"<td valign=\"middle\" align=\"center\"> </td>" +
						"<td valign=\"middle\" align=\"center\"> </td>" +
						"<td valign=\"middle\" align=\"center\">" + allQuestAnswers[i].answers[j].name + "</td>" +
						"<td valign=\"middle\" align=\"center\">" + allQuestAnswers[i].answers[j].pts + "</td>" +
					"</tr>";	
					Empty += answerForTable;
				}
			}
		}
		Empty += "</table>";
		$( "#questionTablePlaceholder" ).append( Empty );
	}
	
	$(function() {
		fillTable();
		
		$("#myCategory").change(function() {
			fillTable();
		});

	});	
	
	</script>
</head> 

<body> 
<table width="100%">
	<tr>
		<td width="236"><img src="../images/Logo-280px.png" alt="LfDI Logo" border="0" height=58px></td>
		<td align="right" style="background:url(../images/farbverlauf.png) right top no-repeat;" border="0"></td>
		<td width="565" align="right" style="background:url(../images/zahlenreihe_058px.png) left top no-repeat;" border="0"></td>
	</tr>
</table>';

//Session-Variable pruefen
//falls diese nicht stimmt:
if(!isset($_SESSION["name"])) 
   { 
   echo "<h2>Bitte erst <a href=\"../index.php\">einloggen</a></h2>"; 
   exit; 
   }    
//falls diese stimmt: 
else{

echo '
	<h1> Fragen der Datenschutz App</h1>
	<br>
	<a href="input.php"><img src="../images/pfeil_r.png" width="35px" align="center"><div id=pfeil>Weitere Fragen eingeben</div></a>
	<br><br><br>
	<h3>Folgende Fragen befinden sich momentan im Fragen-Pool: </h3><br>
	<select id="myCategory" name="myCategory">
		<option value="All">All</option>';
		foreach ($categoryArray as $tmpCatName) {
echo '
		<option value="'. $tmpCatName .'">'.$tmpCatName.'</option>
';
		}
echo '
	</select>
	<br><br>
	<div id="content">
		<div id="questionTablePlaceholder"> </div>
		<br>
		<a href="logout.php"><img src="../images/pfeil_r.png" width="35px" align="center"><div id=pfeil>Ausloggen</div></a>
		<br>
';

}//else

echo '
</body>
</html>';
?>